package com.market.web.model;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.primefaces.model.SelectableDataModel;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.market.domain.PickupSchedule;
import com.market.domain.Picture;
import com.market.domain.Product;
import com.market.domain.PurchaseSchedule;
import com.market.domain.Supplier;
import com.market.domain.transients.DAY_OF_WEEK;
import com.market.util.CollectionUtil;

@Component("productModel")
@Scope("session")
public class ProductModel extends GenericModel<Product>{
	
	
	
	
	@Autowired
	private SupplierModel supplierModel;
	private List<PurchaseSchedule> allPurchaseSchedules;
	
	
	private List<Product> notFreshHaveInventoryList = new ArrayList<Product>();
	
	
	//This is the inventory using for organic inventory restoration;
	private Long organicInventory = 12L;
		
	public Long getOrganicInventory() {
		return organicInventory;
	}

	public void setOrganicInventory(Long organicInventory) {
		this.organicInventory = organicInventory;
	}

	//This is the inventory using for Tofu inventory restoration;
	private Long tofuInventory = 12L ;

	public Long getTofuInventory() {
		return tofuInventory;
	}

	public void setTofuInventory(Long tofuInventory) {
		this.tofuInventory = tofuInventory;
	}

	public List<Product> getNotFreshHaveInventoryList() {
		return notFreshHaveInventoryList;
	}

	public void setNotFreshHaveInventoryList(List<Product> notFreshHaveInventoryList) {
		this.notFreshHaveInventoryList = notFreshHaveInventoryList;
	}
	

	public List<PurchaseSchedule> getAllPurchaseSchedules() {
		return allPurchaseSchedules;
	}
	
	@Override
	public Product getNewObject() {
		Product pdt = new Product();
		pdt.setIsNew(true);
		return pdt;
	}

	public List<SelectItem> getAvailableStatuses() {
		List<SelectItem> statuses = new ArrayList<SelectItem>();
		for (Product.STATUS status : Product.STATUS.values()) {
			statuses.add(new SelectItem(status, status.getLabel()));
		}
		return statuses;
	}

	public List<SelectItem> getAvailableTypes() {
		List<SelectItem> types = new ArrayList<SelectItem>();
		for (Product.TYPE type : Product.TYPE.values()) {
			types.add(new SelectItem(type, type.getLabel()));
		}
		return types;
	}
	
	

	public List<SelectItem> getAvailableSources() {
		List<SelectItem> sources = new ArrayList<SelectItem>();
		for (Product.SOURCE source : Product.SOURCE.values()) {
			sources.add(new SelectItem(source, source.getLabel()));
		}
		return sources;
	}

	/**
	 * return All product supply by specific supplier, the supplier is specify
	 * by SupplierModel's current property; @
	 */
	public List<Product> getProductsBySupplier() {
		List<Product> products = new ArrayList<Product>();
		Supplier supplier = supplierModel.getCurrent();
		for (Product product : this.getList()) {
			if (product.getSupplier().equalsId(supplier)) {
				products.add(product);
			}
		}
		return products;
	}

	/**
	 * This method get all available purchase days(from Sunday to Monday), so we
	 * can set purchase day for specific product;
	 * 
	 * @return
	 */
	private List<PurchaseSchedule> getAvailablePurchaseSchedules() {
		List<PurchaseSchedule> purchaseSchedule = new ArrayList<PurchaseSchedule>();
		for (DAY_OF_WEEK day : DAY_OF_WEEK.values()) {
			PurchaseSchedule schedule = new PurchaseSchedule();
			schedule.setDayOfWeek(day);
			schedule.setIsNew(true);
			schedule.setSelected(false);
			purchaseSchedule.add(schedule);
		}
		return purchaseSchedule;
	}

	/**
	 * This method first get List of all available purchase schedules (from
	 * Sunday to Monday), those elements that exist in current product's
	 * purchase schedules, will point to element in current product's element;
	 * This method actually marked current product's purchase schedules on all
	 * available purchase schedules. so we can show it and change it;
	 * 
	 * @return
	 */
	public List<PurchaseSchedule> getCurrentPurchaseSchedules() {
		allPurchaseSchedules = this.getAvailablePurchaseSchedules();
		List<PurchaseSchedule> currentSchedules = this.getCurrent()
				.getPurchaseSchedules();
		for (PurchaseSchedule currentSchedule : currentSchedules) {
			for (int i = 0; i < allPurchaseSchedules.size(); i++) {
				if (allPurchaseSchedules.get(i).getDayOfWeek() == currentSchedule
						.getDayOfWeek()) {
					allPurchaseSchedules.set(i, currentSchedule);
					i = allPurchaseSchedules.size();
				}
			}
		}
		// CollectionUtil<PickupCalendar> util = new
		// CollectionUtil<PickupCalendar>();
		// allPickupDays = util.markList(allPickupDays, curDays);
		return allPurchaseSchedules;
	}

}
